package
{
	
	import com.shadow.parameters.FlashVars;
	import com.shadow.ui.director.DisplayDirector;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 * 全局环境 在进行初始化的时候会进行对游戏层级结构的初始化,在游戏内部调用层级时只需要用LayerStructure的静态方法去获取对应的层级应用即可
	 * 此环境同时会记录一些基础的游戏状态比如服务器信息等等
	 * @author Liseen
	 * 
	 */	
	public class GlobalContext
	{
		public static const FRAME_RATE:int = 30;
		public static const RESOURCE_PATH:String = "resource/";
		
		private static var _instance:GlobalContext;
		
		private var _root:Sprite;
		private var _stage:Stage;
		private var _stageWidth:Number;
		private var _stageHeight:Number;
		private var _context:LoaderContext;
		private var _flashVars:FlashVars;
		
		public function GlobalContext(value:Singleton)
		{
			
		}
		/**
		 *实例化全局环境 
		 * @param root	根
		 * @return 		GlobalContext
		 * 
		 */		
		public static function getInstance(root:Sprite):GlobalContext
		{
			if(GlobalContext._instance == null)
			{
				GlobalContext._instance = new GlobalContext(new Singleton);
				GlobalContext._instance.init(root);
			}
			
			return GlobalContext._instance;
		}
		/**
		 *获取舞台引用 
		 * @return 
		 * 
		 */		
		public static function get stage():Stage
		{
			return _instance._stage;
		}
		/**
		 *获取舞台宽度 
		 * @return 
		 * 
		 */		
		public static function get stageWidth():Number
		{
			return _instance._stageWidth;
		}
		/**
		 *获取舞台高度 
		 * @return 
		 * 
		 */		
		public static function get stageHeight():Number
		{
			return _instance._stageHeight
		}
		/**
		 *加载上下文
		 * @return 
		 * 
		 */		
		public static function get loaderContext():LoaderContext
		{
			return _instance._context;
		}
		/**
		 *设置flashVars 
		 * @param parameters
		 * 
		 */		
		public static function setFlashVars(parameters:Object):void
		{
			_instance._flashVars = new FlashVars();
			_instance._flashVars.serverURL = parameters["serverURL"];
			_instance._flashVars.userID = parameters["userID"];
			_instance._flashVars.hashCode = parameters["hashCode"];
			_instance._flashVars.registerTime = parameters["registerTime"];
			_instance._flashVars.soundURL = parameters["soundURL"];
			_instance._flashVars.imgURL = parameters["imgURL"];
			_instance._flashVars.version = parameters["version"];
		}
		
		/**
		 * 获取flashVars
		 * @return 
		 * 
		 */		
		public static function get flashVars():FlashVars
		{
			return _instance._flashVars;
		}
		
		
		private function init(root:Sprite):void
		{
			this._root = root;
			this._root.stage.frameRate = FRAME_RATE;
			this._root.stage.scaleMode = StageScaleMode.NO_SCALE;
			this._root.stage.align = StageAlign.TOP_LEFT;
			DisplayDirector.getInstance().stage = this._root
			this._root.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle,false,0,true);
		}
		
		
		private function onAddToStageHandle(event:Event):void
		{
			this._stage = this._root.stage;
			this._stageWidth = this._stage.stageWidth;
			this._stageHeight = this._stage.stageHeight;
			this._context = new LoaderContext(false, ApplicationDomain.currentDomain);
			this._stage.addEventListener(Event.RESIZE,onStageResizeHandle);
			
		}
		
		
		private function onStageResizeHandle(event:Event):void
		{
			this._stageWidth = this._stage.stageWidth;
			this._stageHeight = this._stage.stageHeight;
		}
	}
}

class Singleton 
{
	
	
}

